<template>
  <div>
    <div v-if="cartList!=null&&cartList!=''">
      <div class="page-header">
        <div class="title-opts">
          <h2>
            购物车(
            <em>{{ cartList.length }}</em>)
          </h2>
          <span>管理</span>
        </div>
        <div class="head-bar">
          <p>
            共
            <em>{{ cartList.length }}</em>件宝贝
          </p>
        </div>
      </div>
      <div class="page-content">
        <div class="dinamicx-card-group" v-if="cartList">
          <div class="dian" v-for="(item,index) in cartList" :key="index">
            <h2>
              <van-checkbox v-model="item.shopchecked" @click="change(index)"></van-checkbox>
              <img src="@/assets/images/index/nav1.png" alt />
              <p>{{ item.goods[0].shops }}></p>
            </h2>
            <div class="FrameLayout" v-for="ele in item.goods" :key="ele.cid">
              <div class="goodsBtn">
                <van-checkbox v-model="ele.goodschecked"></van-checkbox>
              </div>
              <div class="goodsImg">
                <img :src="JSON.parse(ele.bannerImg)[0]" alt />
              </div>
              <div class="goodsContemt">
                <p>{{ ele.title }}</p>
                <span>{{ ele.fashion }}</span>
                <div class="num-box">
                  <p class="price">
                    ￥
                    <em>{{ ele.price }}</em>
                  </p>
                  <van-stepper
                    :value="ele.num"
                    async-change
                    input-width="24px"
                    input-height="20px"
                    button-size="24px"
                    @change="onChange($event,ele.goodsId)"
                  />
                </div>
                <div class="deleteGoogs">
                  <p @click="deleteGoogs(ele.goodsId)">删除</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="settlement-bar">
        <van-checkbox v-model="Allchecked">全选</van-checkbox>
        <van-submit-bar :price="TotalPrice" button-text="提交订单" @submit="placeOrder" />
      </div>
    </div>
    <div v-if="cartList==null||cartList==''">
      <div class="page-header">
        <div class="title-opts">
          <h2>
            购物车(
            <em>{{cartList?cartList.length:"0 "}}</em>)
          </h2>
          <span>管理</span>
        </div>
      </div>
      <div class="kong">
        <img src="@/assets/images/cart/kongcart.webp" alt />
        <p>购物车竟然是空的</p>
        <span>再忙，也要记得买点什么犒赏自己~</span>
        <router-link :to="{name:'index'}">
          <div>去逛逛</div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import indexApi from "@/api/indexApi";
import { getUser } from "@/utils/auth";
import { Toast } from "vant";
export default {
  data() {
    return {
      shopchecked: false,
      //Allchecked: false,
      goodschecked: false,
      num: 1,
      //总价TotalPrice
      TotalPrice: 0,
      nameinf: null,
      cartList: null
    };
  },

  components: {},

  methods: {
    //点击数量
    async onChange(value, goodsId) {
      try {
        let p = await indexApi.setGoodscart(
          goodsId,
          value,
          this.nameinf.username
        );
        if (p.data.flag) {
          Toast.clear();
          this.cartList.forEach(item => {
            item.goods.forEach(ele => {
              if (ele.goodsId == goodsId) {
                ele.num = value;
              }
            });
          });
        } else {
          Toast.clear();
          this.$notify({ type: "warning", message: "修改失败" });
        }
      } catch (err) {
        this.$notify({ type: "warning", message: "服务器出错" });
      }
    },
    updatanum(value) {
      console.log(value);
    },
    //提交订单
    placeOrder() {
      console.log("123");
    },
    //二级控三
    change(index) {
      this.cartList[index].goods.forEach((item, idx) => {
        item.goodschecked = !this.cartList[index].shopchecked;
      });
    },
    //获取购物车商品
    async getallCart() {
      try {
        let p = getUser();
        //console.log(p);
        if (p) {
          this.nameinf = JSON.parse(p);
          let a = await indexApi.getcart(this.nameinf.username);
          if (a.data.flag) {
            let value = a.data.data;
            this.cartList = this.saixuan(value);
            console.log(this.cartList);
          }
        }
      } catch (err) {
        console.log(err);
      }
    },
    //筛选店铺
    saixuan(value) {
      //购物车
      let arr = [];
      value.forEach(item => {
        let result = arr.filter(ele => ele.shopName == item.shops);
        if (result.length == 0) {
          arr.push({ shopName: item.shops, goods: [], shopchecked: false });
        }
      });
      value.forEach(item => {
        arr.forEach(ele => {
          if (ele.shopName == item.shops) {
            item.goodschecked = false;
            ele.goods.push(item);
          }
        });
      });
      return arr;
    },
    //删除商品
    async deleteGoogs(goodsId) {
      try {
        this.$dialog
          .confirm({
            message: "我这么可爱你真的舍得删除吗？"
          })
          .then(async () => {
            let p = await indexApi.deleteGoodscart(
              goodsId,
              this.nameinf.username
            );
            if (p.data.flag) {
              this.$notify({ type: "warning", message: "删除成功" });
              this.cartList.forEach((item, index) => {
                item.goods.forEach((ele, idx) => {
                  if (ele.goodsId == goodsId) {
                    this.cartList[index].goods.splice(idx, 1);
                    if (!this.cartList[index].goods.length) {
                      this.cartList.splice(index, 1); //如果该商品没有商品，则删除整个店铺
                    }
                  }
                });
              });
              console.log(this.cartList);
            }
          })
          .catch(() => {
            // on cancel
          });
      } catch (err) {
        console.log(err);
      }
    }
  },
  computed: {
    //一三级
    Allchecked: {
      get: function(newnum) {
        let arr = [];
        this.cartList.forEach(item => {
          let res = item.goods.every(ele => ele.goodschecked == true);
          arr.push(res);
        });
        return arr.every(item => item == true);
      },
      set: function(newValue) {
        this.cartList.forEach(item => {
          item.goods.forEach(ele => (ele.goodschecked = newValue));
          console.log(this.cartList);
        });
      }
    }
  },
  watch: {
    cartList: {
      deep: true,
      handler(newval) {
        //三控二
        newval.forEach(item => {
          item.shopchecked = item.goods.every(ele => ele.goodschecked == true);
        });
        //TotalPrice总价
        this.TotalPrice = 0;
        newval.forEach(item => {
          item.goods.forEach(ele => {
            if (ele.goodschecked) {
              this.TotalPrice += ele.num * 1 * (ele.price * 1) * 100;
            }
          });
        });
      }
    }
  },
  created() {
    this.getallCart();
  }
};
</script>

<style scoped lang="scss">
//头部
.page-header {
  width: 100%;
  height: 177px;
  background-image: -webkit-linear-gradient(right, #ff5000 0%, #ff8400 100%);
  background-image: linear-gradient(-90deg, #ff5000 0%, #ff8400 100%);
  padding: 20px 12px;
  color: #fff;
  .title-opts {
    display: flex;
    justify-content: space-between;

    h2 {
      font-size: 20px;

      font-weight: 600;
    }
    span {
      font-size: 14px;
    }
  }
  .head-bar {
    margin-top: 10px;
    font-size: 12px;
  }
}
//空淘宝
.kong {
  text-align: center;
  padding-top: 15px;
  img {
    margin: 0 auto;
    width: 110px;
    height: 110px;
  }
  p {
    margin: 10px 0;
    font-size: 16px;
    color: rgb(102, 102, 102);
  }
  span {
    font-size: 14px;
    color: rgb(192, 191, 191);
  }
  div {
    margin: 0 auto;
    width: 82px;
    height: 37px;
    margin-top: 16px;
    color: rgb(153, 153, 153);
    border: 1px solid rgb(240, 240, 240);
    box-shadow: rgb(221, 221, 221) 0px 0px 0px 1px inset;
    line-height: 37px;
  }
}
//购物车商品
.page-content {
  width: 100%;
  padding: 0 12px 60px;
  margin-top: -85px;
  .dinamicx-card-group {
    .dian {
      background-color: #fff;
      margin-bottom: 10px;
      border-radius: 15px;
      h2 {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        height: 50px;
        border-bottom: 0.5px solid rgb(241, 241, 241);
        .van-checkbox {
          width: 40px;
          display: flex;
          justify-content: center;
        }
        img {
          width: 30px;
          height: 30px;
          position: relative;
          top: -3px;
        }
        p {
          font-size: 12px;
        }
      }
      .FrameLayout {
        display: flex;
        padding-top: 5px;
        .goodsBtn {
          width: 40px;
          height: 90px;
          display: flex;
          justify-content: center;
        }
        .goodsImg {
          width: 90px;
          height: 90px;
          img {
            width: 100%;
            height: 100%;
          }
        }
        .goodsContemt {
          padding: 0 12px 20px;
          flex: 1;
          p {
            font-size: 14px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }
          span {
            font-size: 12px;
            height: 24px;
            text-align: center;
            line-height: 24px;
            margin: 7px 0;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            color: rgb(153, 153, 153);
            background-color: rgba(236, 236, 236, 0.298);
          }
          .num-box {
            height: 20px;
            transform: translateZ(0);
            display: flex;
            justify-content: space-between;
            p {
              font-size: 14px;
              color: rgb(255, 85, 0);
            }
            .van-stepper {
              height: 25px;
              transform: translateZ(0);
              display: flex;
              background-color: #fff;
            }
          }
          .deleteGoogs {
            margin-top: 10px;
            display: flex;
            justify-content: flex-end;
            color: #ff5000;
          }
        }
      }
    }
  }
}
//提交订单
.settlement-bar {
  height: 50px;
  background-color: rgb(255, 255, 255);
  position: fixed;
  bottom: 50px;
  width: 100%;
  .van-checkbox {
    width: 100px;
    position: absolute;
    left: 8px;
    top: 15px;
    z-index: 999;
    font-size: 14px;
  }
  .van-submit-bar {
    bottom: 50px;
  }
}
</style>
